package com.zwz.crawler.tendering.getBigInfo;


/**
 * 投标信息，招标信息暂时查询不到无法入库
 *
 */
import com.zwz.constant.Constant;
import com.zwz.parse.JsonParserUtil;
import com.zwz.vo.BidInfo;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.List;

@Slf4j
public class TungeeBiddingSearch {
    private static final String BOUNDARY = "----WebKitFormBoundaryB0BrAnUXfdvoWivu";

    public static  List<BidInfo>  startTungeeBiddingSearch(String enterpriseId ) throws IOException {
        // 构建 multipart 请求体
        String formData = buildMultipartBody();

        Connection.Response response = Jsoup.connect("https://sales.tungee.com/api/winning/detail/search")
                .method(Connection.Method.POST)
                .requestBody(formData)
                // 严格设置请求头
                .header("authority", "sales.tungee.com")
                .header("accept", "*/*")
                .header("accept-encoding", "gzip, deflate, br, zstd")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "no-cache")
                .header("content-type", "multipart/form-data; boundary=" + BOUNDARY)
                .header("cookie", Constant.cookies)
                .header("origin", "https://sales.tungee.com")
                .header("pragma", "no-cache")
                .header("priority", "u=1, i")
                .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/bidding")
                .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "empty")
                .header("sec-fetch-mode", "cors")
                .header("sec-fetch-site", "same-origin")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                .ignoreContentType(true)
                .ignoreHttpErrors(true)
                .timeout(30000)
                .execute();

       log.info("Status: " + response.statusCode());
       log.info("Response: " + response.body());

        //解析对应的数据
        List<BidInfo> res = JsonParserUtil.parsetoubiao(response.body());

        return res;

    }

    private static String buildMultipartBody() {
        return new StringBuilder()
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"nameId\"\r\n\r\n")
                .append("577de19eadf21dc4\r\n")
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"begin\"\r\n\r\n")
                .append("0\r\n")
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"end\"\r\n\r\n")
                .append("5\r\n")
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"filter\"\r\n\r\n")
                .append("{}\r\n")  // JSON空对象
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"sortField\"\r\n\r\n")
                .append("publishDate\r\n")
                .append("--").append(BOUNDARY).append("\r\n")
                .append("Content-Disposition: form-data; name=\"sort\"\r\n\r\n")
                .append("0\r\n")
                .append("--").append(BOUNDARY).append("--\r\n")
                .toString();
    }


}



